package com.youngball.business.dao.redis.task;

import com.dyuproject.protostuff.LinkedBuffer;
import com.dyuproject.protostuff.ProtostuffIOUtil;
import com.dyuproject.protostuff.runtime.RuntimeSchema;
import com.youngball.business.dao.redis.BaseRedisDao;
import com.youngball.business.entity.AutoLightTask;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * Created by liaopingzhu on 2017/1/12.
 */
//@Repository("taskRedisDao")
public class TaskRedisDao extends BaseRedisDao<Object,Object> {

    private final JedisPool jedisPool;

    private RuntimeSchema<AutoLightTask> schema = RuntimeSchema.createFrom(AutoLightTask.class);

    public TaskRedisDao(String ip, int port) {
        jedisPool = new JedisPool(ip, port);
    }

    /**
     * 设置定时任务到redis中
     * @param autoLightTask
     * @return
     */
    public String setCloseLightJob(AutoLightTask autoLightTask){
        try {
            Jedis jedis = jedisPool.getResource();
            try {
                String key = "handlight:" + autoLightTask.getUuid();
                byte[] bytes = ProtostuffIOUtil.toByteArray(autoLightTask, schema,
                        LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE));
                // 超时缓存设为一天
                int timeout = 60 * 60 * 24;
                String result = jedis.setex(key.getBytes(),timeout,bytes);
                //利用redis订阅发布功能处理定时任务
                jedis.expireAt(key, autoLightTask.getTime());
                return result;
            }finally {
                jedis.close();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 从redis中取出定时任务
     * @param uuid
     * @return
     */
    public AutoLightTask getCloseLightJob(String uuid){
        try {
            Jedis jedis = jedisPool.getResource();
            try {
                String key = "handlight:" + uuid;
                // get -> byte[] -> 反序列化 -> object[Seckill]
                // 采用自定义序列化
                // protostuff : pojo.
                byte[] bytes = jedis.get(key.getBytes());
                if (bytes != null) {
                    AutoLightTask autoLightTask = schema.newMessage();
                    ProtostuffIOUtil.mergeFrom(bytes, autoLightTask, schema);
                    // autoLightTask被反序列化
                    return autoLightTask;
                }
            } finally {
                jedis.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     *  触发定时任务
     */
    public void triggerJob(String key,long time){
        try {
            Jedis jedis = jedisPool.getResource();
            try {
                jedis.expireAt(key, time);
            }finally {
                jedis.close();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 开启redis订阅模式
     */
    public void openRedisSub(){
        try {
            Jedis jedis = jedisPool.getResource();
            try {
//                jedis.psubscribe(new KeyExpiredListener(), "__key*__:*");
            }finally {
                jedis.close();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 删除redis中的元素记录
     */
    public void deleteByKey(String key){
        try {
            Jedis jedis = jedisPool.getResource();
            try {
                jedis.del("handlight:"+key);
            }finally {
                jedis.close();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }



}
